import 'package:flutter/material.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:xingsu/page/publisher/viewModel/activity_more_viewmodel.dart';
import 'package:xingsu/util/app_util.dart';
import 'package:xingsu/util/image_util.dart';
import 'package:xingsu/widget/back_widget.dart';
import 'package:get/get.dart';

class ActivityMorePage extends StatefulWidget {
  const ActivityMorePage({Key? key}) : super(key: key);

  @override
  State<ActivityMorePage> createState() => _ActivityMorePageState();
}

class _ActivityMorePageState extends State<ActivityMorePage> {
  final ActivityMoreViewModel _viewModel = Get.put(ActivityMoreViewModel());
  CalendarFormat _calendarFormat = CalendarFormat.month;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: UnconstrainedBox(
          child: BackWidget(),
        ),
        title: const Text("活动日历"),
      ),
      body: ListView(
        children: [_calendarWidget(), _switchWidget(), _itemWidget()],
      ),
    );
  }

  Widget _itemWidget() {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 20),
      decoration: BoxDecoration(
          color: const Color(0xFF292B34),
          borderRadius: BorderRadius.circular(5)),
      padding: const EdgeInsets.all(10),
      child: Column(
        children: [
          loadNetworkImage(
              "https://img0.baidu.com/it/u=1871742084,1283176001&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1673456400&t=6714061ffb706ec476c2dfcea85ccd75"),

        ],
      ),
    );
  }

  Widget _switchWidget() {
    return AnimatedSwitcher(
      duration: const Duration(milliseconds: 200),
      transitionBuilder: (Widget child, Animation<double> animation) {
        return ScaleTransition(scale: animation, child: child);
      },
      child: _calendarFormat == CalendarFormat.month
          ? IconButton(
              key: const ValueKey(true),
              onPressed: () {
                setState(() {
                  _calendarFormat = CalendarFormat.week;
                });
              },
              icon: const Icon(
                Icons.arrow_drop_up,
                size: 30,
                color: Color(0xFF8B8D97),
              ))
          : IconButton(
              key: const ValueKey(false),
              onPressed: () {
                setState(() {
                  _calendarFormat = CalendarFormat.month;
                });
              },
              icon: const Icon(
                Icons.arrow_drop_down,
                size: 30,
                color: Color(0xFF8B8D97),
              )),
    );
  }

  Widget _calendarWidget() {
    return TableCalendar(
      locale: "zh",
      headerVisible: false,
      firstDay: AppUtil.lastMonthFirstDay(),
      lastDay: AppUtil.nextMonthLastDay(),
      daysOfWeekHeight: 40,
      focusedDay: _viewModel.focusedDay,
      calendarFormat: _calendarFormat,
      calendarStyle: const CalendarStyle(
        outsideTextStyle:
            TextStyle(color: Color(0xFF585C69), fontWeight: FontWeight.w700),
        defaultTextStyle:
            TextStyle(color: Colors.white, fontWeight: FontWeight.w700),
        weekendTextStyle:
            TextStyle(color: Colors.white, fontWeight: FontWeight.w700),
        disabledTextStyle:
            TextStyle(color: Color(0xFF585C69), fontWeight: FontWeight.w700),
      ),
      daysOfWeekStyle: const DaysOfWeekStyle(
          weekendStyle:
              TextStyle(color: Color(0xFF83858D), fontWeight: FontWeight.w700),
          weekdayStyle:
              TextStyle(color: Color(0xFF83858D), fontWeight: FontWeight.w700)),
      shouldFillViewport: false,
      formatAnimationCurve: Curves.easeInOutCubic,
      headerStyle:
          const HeaderStyle(formatButtonVisible: false, titleCentered: true),
      selectedDayPredicate: (day) {
        return isSameDay(_viewModel.selectedDay, day);
      },
      onDaySelected: (selectedDay, focusedDay) {
        if (!isSameDay(_viewModel.selectedDay, selectedDay)) {
          setState(() {
            _viewModel.selectedDay = selectedDay;
            _viewModel.focusedDay = focusedDay;
          });
        }
      },
      onFormatChanged: (format) {},
    );
  }
}
